<?php
// header("Content-type:text/html;charset=utf-8"); 
	
	class Model{
		//成员属性
		protected $link;//连接资源
		protected $tablename;//表名
		protected $fields=array();//字段属性
		protected $pk;//主键
		protected $limit=null;//主键
		protected $where=array();//搜索条件
		protected $sqlfree="";//自定义sql语句




		//构造方法
		public function __construct($tablename){
			// var_dump($tablename);
			$this->tablename=$tablename;
			//连接数据库
			$this->link=mysqli_connect(DB_HOST,DB_USER,DB_PASS)or die('数据连接失败');
			//选库
			mysqli_select_db($this->link,DB_NAME);
			//设置字符集
			mysqli_set_charset($this->link,'utf8');
			$this->field();
		}

		//获取数据库里面字段信息

		public function field(){
			// echo "111";
			//准备sql语句
			$sql="desc {$this->tablename}";
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//解析
			while($rows=mysqli_fetch_assoc($result)){
				// var_dump($rows);
				//把除了id以外的所有字段信息写入数组 $fields
				if($rows['Key']=='PRI'){
					$this->pk=$rows['Field'];
				}else{
					$this->fields[]=$rows['Field'];
				}

			}
			// var_dump($this->fields);
		}
		//添加
		public function insert($data=array()){
			// var_dump($data);
			$data=$_POST;
			// var_dump($data);
			// die();
			$listkey=array();
			$listvalue=array();

			//遍历
			foreach($data as $key=>$value){
				// var_dump($value);
				//过滤无效字段
				if(in_array($key,$this->fields)){
					$listkey[]=$key;
					$listvalue[]=$value;
				}
				
			}
			// var_dump($listkey);
			// var_dump($listvalue);

			//准备sql语句
			$sql="insert into {$this->tablename}(".implode(",",$listkey).")
			values('".implode("','",$listvalue)."')";
			// echo $sql;
			// insert into stu (name,age,sex,classid)values('fang','25','m','146')
			//执行sql语句
			mysqli_query($this->link,$sql);
			return mysqli_insert_id($this->link);
		}
		//计算数据总条数
		public function count(){
			//准备sql语句
			$sql="select count(*) from {$this->tablename}";
			// select count(*) from stu where name like 'h' and sex='m';
			// 拼接sql语句(求出搜索出来的总条数)
			if(count($this->where)>0){
				$sql.=" where ".implode(" and ",$this->where);
				// echo $sql;
			}
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//获取结果集
			return mysqli_fetch_row($result)[0];
		}
		public function count1($sqlfree1){
			//准备sql语句
		
			$sql=$sqlfree1;
		// $sql = "select count(*) as total , g.*,t.name from goods g,type t  where g.typeid=t.id";
			// 拼接sql语句(求出搜索出来的总条数)
			if(count($this->where)>0){
				$sql.=" and ".implode(" and ",$this->where);
				// echo $sql;
			}
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//获取结果集
			return mysqli_fetch_row($result)[0];
		}
		//查询数据
		public function select(){
			//准备sql语句
			$sql="select * from {$this->tablename}" ;
			// 拼接sql语句
			// select * from stu where name like 'h' and sex='m' limit 0,4;

			if(count($this->where)>0){
				$sql.=" where ".implode(" and ",$this->where);
				// echo $sql;
			}
			//拼接sql
			if(!empty($this->limit)){
				$sql.=" limit ".$this->limit;
				// echo $sql;
			}

			
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//获取结果集
			$a=array();
			while($row=mysqli_fetch_assoc($result)){
				$a[]=$row;
			}
			return $a;
		}
		//查询数据
		public function type(){
			//准备sql语句
			$sql="select * from {$this->tablename} order by concat(path,id)" ;
			// select * from type order by concat(path,id);
			// 拼接sql语句
			// select * from stu where name like 'h' and sex='m' limit 0,4;

			if(count($this->where)>0){
				$sql.=" where ".implode(" and ",$this->where);
				// echo $sql;
			}
			//拼接sql
			if(!empty($this->limit)){
				$sql.=" limit ".$this->limit;
				// echo $sql;
			}

			
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//获取结果集
			$a=array();
			while($row=mysqli_fetch_assoc($result)){
				$a[]=$row;
			}
			return $a;
		}
			//查询数据
		public function free($sqlfree1){
			$this->sqlfree=$sqlfree1;
		
			if(!empty($this->sqlfree)){
				$sql=$this->sqlfree;
			}
	

			if(count($this->where)>0){
				$sql.=" and ".implode(" and ",$this->where);
				// echo $sql;
			}
			//拼接sql
			if(!empty($this->limit)){
				$sql.=" limit ".$this->limit;
				// echo $sql;
			}

			
			//执行sql语句
			$result=mysqli_query($this->link,$sql);
			//获取结果集
			$a=array();
			while($row=mysqli_fetch_assoc($result)){
				$a[]=$row;
			}
			return $a;
		}
		//删除
		public function del($id){
			//准备sql语句
			$sql="delete from {$this->tablename} where {$this->pk}=$id";
			//执行sql语句
			mysqli_query($this->link,$sql);
			//返回受影响的行数
			return mysqli_affected_rows($this->link);
		}
		//执行修改
		public function update($data=array()){
			$data=$_POST;
			//遍历
			$a=array();

			foreach($data as $key=>$value){
				//过滤无效字段
				if(in_array($key,$this->fields)){
				
					$a[]="{$key}='{$value}'";
				}
				
			}
			//准备sql语句
			$sql="update {$this->tablename} set ".implode(",",$a)." where {$this->pk}={$_POST['id']} ";
			//执行sql
			mysqli_query($this->link,$sql);
			//返回受影响的行数
			return mysqli_affected_rows($this->link);
		}
		//查找单条数据信息
		public function find($id){
			//准备sql语句
			$sql="select * from {$this->tablename} where {$this->pk}=$id";
			//执行sql
			$result=mysqli_query($this->link,$sql);
			if($result&&mysqli_affected_rows($this->link)){
				$s=mysqli_fetch_assoc($result);
				return $s;
			}
			return null;
		}

		public function limit($m){
			$this->limit=$m;
			// echo $this->limit;
			return $this;
		}
		public function where($where){
			// var_dump($where);
			//的到的搜索条件  赋给 $where 数组
			$this->where[]=$where;
			// var_dump($this->where);
		}
		//关闭数据库
		public function __destruct(){
			mysqli_close($this->link);
		}
	} 
 ?>